// 主JavaScript文件 - 超市管理系统

// 文档加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    console.log('超市管理系统已加载');
    
    // 添加一些简单的交互效果
    addHoverEffects();
    setupButtons();
});

// 添加悬停效果
function addHoverEffects() {
    // 为所有卡片添加悬停效果
    const cards = document.querySelectorAll('.card');
    cards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-5px)';
            this.style.boxShadow = '0 8px 16px rgba(0, 0, 0, 0.1)';
        });
        
        card.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
            this.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.1)';
        });
    });
}

// 设置按钮点击事件
function setupButtons() {
    // 获取所有按钮
    const buttons = document.querySelectorAll('.btn');
    
    buttons.forEach(button => {
        button.addEventListener('click', function() {
            // 添加点击效果
            this.style.transform = 'scale(0.98)';
            setTimeout(() => {
                this.style.transform = 'scale(1)';
            }, 100);
            
            // 如果是加入购物车按钮
            if (this.textContent.includes('加入购物车')) {
                alert('商品已成功加入购物车！');
            }
            
            // 如果是立即购买按钮
            if (this.textContent.includes('立即购买')) {
                alert('即将跳转到结算页面！');
            }
        });
    });
}

// 简单的表单验证函数
export function validateForm(formId) {
    const form = document.getElementById(formId);
    if (!form) return false;
    
    let isValid = true;
    const requiredFields = form.querySelectorAll('[required]');
    
    requiredFields.forEach(field => {
        if (!field.value.trim()) {
            isValid = false;
            field.style.border = '2px solid #e74c3c';
        } else {
            field.style.border = '1px solid #ddd';
        }
    });
    
    return isValid;
}

// 显示通知消息的函数
export function showNotification(message, type = 'success') {
    // 检查是否已存在通知元素
    let notification = document.querySelector('.notification');
    
    if (!notification) {
        // 创建新的通知元素
        notification = document.createElement('div');
        notification.className = 'notification';
        document.body.appendChild(notification);
        
        // 添加CSS样式
        const style = document.createElement('style');
        style.textContent = `
            .notification {
                position: fixed;
                top: 20px;
                right: 20px;
                padding: 15px 20px;
                border-radius: 4px;
                color: white;
                font-weight: bold;
                z-index: 1000;
                animation: slideIn 0.3s ease-out;
            }
            
            .notification.success {
                background-color: #4CAF50;
            }
            
            .notification.error {
                background-color: #e74c3c;
            }
            
            .notification.info {
                background-color: #2196F3;
            }
            
            @keyframes slideIn {
                from {
                    transform: translateX(100%);
                    opacity: 0;
                }
                to {
                    transform: translateX(0);
                    opacity: 1;
                }
            }
        `;
        document.head.appendChild(style);
    }
    
    // 设置消息内容和类型
    notification.textContent = message;
    notification.className = 'notification';
    notification.classList.add(type);
    
    // 3秒后自动隐藏
    setTimeout(() => {
        notification.style.opacity = '0';
        notification.style.transform = 'translateX(100%)';
        notification.style.transition = 'opacity 0.3s, transform 0.3s';
    }, 3000);
}

// 简单的AJAX请求函数
export function ajaxRequest(method, url, data = null, callback = null) {
    const xhr = new XMLHttpRequest();
    
    xhr.open(method, url, true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    
    xhr.onload = function() {
        if (xhr.status >= 200 && xhr.status < 400) {
            const response = JSON.parse(xhr.responseText);
            if (callback) callback(response);
        }
    };
    
    if (data) {
        xhr.send(JSON.stringify(data));
    } else {
        xhr.send();
    }
}